package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/03 21:31
 **/
public class Day1_5_Offer44_FindNthDigit {
    public static void main(String[] args) {
        System.out.println(findNthDigit(5));
    }
    public static int findNthDigit(int n) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        while(sb.length()-1 < n){
            sb.append(i);
            i++;
        }
        System.out.println(sb);
        char[] arr = sb.toString().toCharArray();
        int res = arr[n] - 48;
        return res;
    }
    class Solution {
        public int findNthDigit(int n) {
            int digit = 1;
            long start = 1;
            long count = 9;
            while(n > count){
                n-=count;
                digit++;
                start *= 10;
                count = digit * start * 9;
            }
            long num = start + (n - 1) / digit;
            return Long.toString(num).charAt((n-1) % digit) - '0';
        }
    }
}